package com.lsongh.springBoot.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
/**
 * 引入AOP
 */
public class HttpAspect {

    private static final Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.lsongh.springBoot.controller.*.*(..))")
    public void log() {

    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
        //logger.warn("开始使用方法前，获取接口信息:");

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url
        logger.warn("url={}", request.getRequestURI());
        //method
        logger.warn("method={}", request.getMethod());
        //ip
        logger.warn("ip={}", request.getRemoteAddr());
        //类方法
        logger.warn("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //参数
        logger.warn("args={}", joinPoint.getArgs());
    }

    @After("log()")
    public void doAfter() {
        //logger.warn("结束使用方法");
    }
    //获取返回的结果
    @AfterReturning(returning = "object",pointcut = "log()")
    public void daAfterReturn(Object object){
        logger.warn("response={}",object.toString());
    }
}
